package ThaumPerditio.client.renderer.tile;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.AdvancedModelLoader;
import net.minecraftforge.client.model.IModelCustom;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import ThaumPerditio.common.tile.TileDefusionPedestal;

@SideOnly(Side.CLIENT)
public class TileDefusionPedestalTESR extends TileEntitySpecialRenderer {
    ResourceLocation texture;
    ResourceLocation objModelLocation;
    IModelCustom model;

    public TileDefusionPedestalTESR() {
        this.objModelLocation = new ResourceLocation("thaumperditio", "textures/models/defusionpedestal.obj");
        this.texture = new ResourceLocation("thaumperditio", "textures/models/defusionpedestal.png");
        this.model = AdvancedModelLoader.loadModel(this.objModelLocation);
    }

    @Override
    public void renderTileEntityAt(TileEntity tileEntity, double posX, double posY, double posZ, float timeSinceTick) {
        bindTexture(texture);

        GL11.glPushMatrix();

        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glTranslated(posX + 1, posY, posZ + 1);
        GL11.glScalef(0.0625F, 0.0625F, 0.0625F);

        GL11.glPushMatrix();

        GL11.glRotatef(0F, 0F, 0F, 0F);
        model.renderAll();

        GL11.glPopMatrix();
        GL11.glPopMatrix();

        if(tileEntity==null || tileEntity.getWorldObj()==null || !(tileEntity instanceof TileDefusionPedestal)) return;

        if (((TileDefusionPedestal) tileEntity).getStackInSlot(0)!=null) {
            final TileDefusionPedestal castedTE = (TileDefusionPedestal) tileEntity;
            EntityItem entityitem = null;
            float ticks = (float) Minecraft.getMinecraft().renderViewEntity.ticksExisted + timeSinceTick;
            GL11.glPushMatrix();
            float h = MathHelper.sin(ticks % 32767.0F / 16.0F) * 0.05F;
            GL11.glTranslatef((float) posX + 0.5F, (float) posY + 1.15F + h, (float) posZ + 0.5F);
            GL11.glRotatef(ticks % 360.0F, 0.0F, 1.0F, 0.0F);
            if (castedTE.getStackInSlot(0).getItem() instanceof ItemBlock) {
                GL11.glScalef(2.0F, 2.0F, 2.0F);
            } else {
                GL11.glScalef(1.0F, 1.0F, 1.0F);
            }

            ItemStack istk = castedTE.getStackInSlot(0).copy();
            istk.stackSize = 1;
            entityitem = new EntityItem(castedTE.getWorldObj(), 0.0D, 0.0D, 0.0D, istk);
            entityitem.hoverStart = 0.0F;
            RenderManager.instance.renderEntityWithPosYaw(entityitem, 0.0D, 0.0D, 0.0D, 0.0F, 0.0F);
            if (!Minecraft.isFancyGraphicsEnabled()) {
                GL11.glRotatef(180.0F, 0.0F, 1.0F, 0.0F);
                RenderManager.instance.renderEntityWithPosYaw(entityitem, 0.0D, 0.0D, 0.0D, 0.0F, 0.0F);
            }

            GL11.glPopMatrix();
        }
    }


}
